package com.kedll.library;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.ImageView;

import com.kedll.library.util.SharePreUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * 继承此类来实现启动页
 *
 * @author QiQi
 * @date 16/1/11
 */
public abstract class AbstractStartActivity extends KBaseActivity {


    @Override
    protected int getLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
//        Window window = getWindow();
//        window.setBackgroundDrawableResource(getImageResource());
        ImageView imageView = findViewById(R.id.image);
        imageView.setImageResource(getImageResource());
    }

    @Override
    protected void initEvent() {
    }

    @SuppressLint("CheckResult")
    @Override
    protected void onResume() {
        super.onResume();
        Observable.just(1)
                .distinct(integer -> {
                    initThirdService();
                    return integer;
                })
                .delay(getDelayTime(), getTimeUnit())
                .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> start());

    }

    /**
     * 切换到指定的Activity， 无传递数据
     *
     * @param cls 指定的Activity
     */
    @Override
    public void startActivity(Class cls) {
        super.startActivity(cls);
        finish();
    }

    /**
     * 初始化
     */
    protected abstract void initThirdService();

    protected int getDelayTime() {
        return 2;
    }

    protected TimeUnit getTimeUnit() {
        return TimeUnit.SECONDS;
    }

    /**
     * 启动主界面
     */
    private void start() {
        if (SharePreUtils.isNewNavigateCode()) {
            SharePreUtils.setNewNavigateCode();
            showNavigateActivity();

        } else {
            showMainActivity();
        }
    }

    /**
     * 显示引导界面
     */
    protected void showNavigateActivity() {
        showMainActivity();
    }

    /**
     * 显示主界面
     */
    protected abstract void showMainActivity();

    /**
     * 设置显示启动页面图片
     *
     * @return
     */
    protected abstract int getImageResource();

}
